#pragma once

#include <linux/rbtree_types.h>

/* bits in flags of vmalloc's vm_struct below */
#define VM_IOREMAP 0x00000001           /* ioremap() and friends */
#define VM_ALLOC 0x00000002             /* vmalloc() */
#define VM_MAP 0x00000004               /* vmap()ed pages */
#define VM_USERMAP 0x00000008           /* suitable for remap_vmalloc_range */
#define VM_DMA_COHERENT 0x00000010      /* dma_alloc_coherent */
#define VM_UNINITIALIZED 0x00000020     /* vm_struct is not fully initialized */
#define VM_NO_GUARD 0x00000040          /* ***DANGEROUS*** don't add guard page */
#define VM_KASAN 0x00000080             /* has allocated kasan shadow memory */
#define VM_FLUSH_RESET_PERMS 0x00000100 /* reset direct map and flush TLB on unmap, can't be freed in atomic context */
#define VM_MAP_PUT_PAGES 0x00000200     /* put pages and free array in vfree */
#define VM_ALLOW_HUGE_VMAP 0x00000400   /* Allow for huge pages on archs with HAVE_ARCH_HUGE_VMALLOC */

#if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
    !defined(CONFIG_KASAN_VMALLOC)
#define VM_DEFER_KMEMLEAK 0x00000800 /* defer kmemleak object creation */
#else
#define VM_DEFER_KMEMLEAK 0
#endif
#define VM_SPARSE 0x00001000 /* sparse vm_area. not all pages are present. */

/* bits [20..32] reserved for arch specific ioremap internals */

/*
 * Maximum alignment for ioremap() regions.
 * Can be overridden by arch-specific value.
 */
#ifndef IOREMAP_MAX_ORDER
#define IOREMAP_MAX_ORDER (7 + PAGE_SHIFT) /* 128 pages */
#endif

struct vm_struct
{
    struct vm_struct *next;
    void *addr;
    unsigned long size;
    unsigned int flags;
    unsigned int nr_pages;
    phys_addr_t phys_addr;
    const void *caller;
    unsigned long requested_size;
};

struct vmap_area
{
    unsigned long va_start;
    unsigned long va_end;

    struct rb_node rb_node; /* address sorted rbtree */
    struct list_head list;  /* address sorted list */

    /*
     * The following two variables can be packed, because
     * a vmap_area object can be either:
     *    1) in "free" tree (root is free_vmap_area_root)
     *    2) or "busy" tree (root is vmap_area_root)
     */
    union
    {
        unsigned long subtree_max_size; /* in "free" tree */
        struct vm_struct *vm;           /* in "busy" tree */
    };
    unsigned long flags; /* mark type of vm_map_ram area */
};
